/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"

#include "ui按钮.h"



static void f_按钮Draw(C_Widget* self, S_2D画布* 画布) {
	C_按钮* box = static_cast<C_按钮*>(self);
	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);

	auto 字体 = f_surface_get字体(*self->m_UIctx, S_UI主题::ui_默认字体);

	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);
	box->m_字符绘制.f_Draw(画布, &字体, box->m_背景绘制.m_属性);
	box->m_图标绘制.f_Draw单色(画布, box->m_背景绘制.m_属性, S_UI主题::ui_默认图标);
}

static void f_按钮变换修改(C_Widget* self) {
	C_按钮* box = static_cast<C_按钮*>(self);

	mat3X2 Mat;
	Mat.row0 = {};
	Mat.row1 = box->m_Size;
	Mat.row2 = { S_UI主题::ui_面板圆角半径*1.5f, 0 };

	box->m_背景绘制.f_alloc(1);
	box->m_背景绘制.f_set单色填充(0, Mat, self->m_颜色A, self->m_图元);
	
	box->m_字符绘制.f_alloc(1, 12);
	box->m_字符绘制.f_绘制文本({ Mat.row0 , Mat.row1 }, S_UI主题::uic_文字颜色, 0, box->m_WName, E_对齐方式::e_据中对齐);
	
	
	
	Mat.row0 = box->m_Size * 0.5f;
	Mat.row1.x = Mat.row1.y = box->m_Size.y * 0.7f;

	box->m_图标绘制.f_alloc(1);
	box->m_图标绘制.f_set(box->m_ICO, 0, Mat, S_UI主题::uic_文字颜色);
	
}




static bool f_按钮单击(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_按钮* but = static_cast<C_按钮*>(self);
	if (but->m_Op.m_UI && but->m_Op.m_UI->m_Update) {
		but->m_Op.m_UI->m_Update(but->m_Op);
	}
	return true;
}



bool on_按钮部件鼠标移入(C_Widget* self, const S_鼠标& 鼠标) {
	if (self->mf_当前部件属性修改) {
		self->mf_当前部件属性修改(self, E_UI绘制调整部分::e_进入);
	}
	C_按钮* box = static_cast<C_按钮*>(self);
	self->m_颜色A = self->m_颜色B;

	if (box->m_背景绘制.m_图元.属性) {
		
		box->m_背景绘制.f_setColor(0, self->m_颜色A);
	}
	return true;
}
bool on_按钮部件鼠标移出(C_Widget* self, const S_鼠标& 鼠标) {
	if (self->mf_当前部件属性修改) {
		self->mf_当前部件属性修改(self, E_UI绘制调整部分::e_退出);
	}
	C_按钮* box = static_cast<C_按钮*>(self);
	self->m_颜色A = self->m_颜色C;

	if (box->m_背景绘制.m_图元.属性) {
		
		box->m_背景绘制.f_setColor(0, self->m_颜色A);
	}
	return true;
}

C_按钮::C_按钮(S_UI创建环境* ctx, S_Props* ops, std::wstring 标题) : C_Widget(ctx) {
	if (ops) {
		m_Op = (*ops);
		m_WName = m_Op.m_Name;
	}
	else {
		m_Op = {};
		m_WName = 标题;
	}

	mf_鼠标按键点击 = f_按钮单击;
	
	mf_DrawFun = f_按钮Draw;
	mf_变换修改 = f_按钮变换修改;

	mf_鼠标进入 = on_按钮部件鼠标移入;
	mf_鼠标离开 = on_按钮部件鼠标移出;
	
	
	
	m_颜色A = S_UI主题::uic_按钮背景;
	m_颜色B = S_UI主题::uic_按钮;
	m_颜色C = S_UI主题::uic_按钮背景;
	
	m_图元 = S_Widget预设图元::m_圆角矩形;

	m_绘制标题 = true;
	m_绘制图标 = false;
	f_setSize({ 22,22 });
}

C_按钮::~C_按钮() {
}





static bool f_开关单击(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_开关* but = dynamic_cast<C_开关*>(self);
	but->m_值 = !but->m_值;
	return true;
}

C_开关::C_开关(S_UI创建环境& ctx) {
	mf_鼠标按键点击 = f_开关单击;

	m_颜色A = S_UI主题::uic_值编辑框背景;
	m_颜色B = S_UI主题::uic_值编辑框边;
}

C_开关::~C_开关() {
}




static void on_标签Draw(C_Widget* self, S_2D画布* 画布) {
	C_标签* box = static_cast<C_标签*>(self);
	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);

	auto 字体 = f_surface_get字体(*self->m_UIctx, S_UI主题::ui_默认字体);
	box->m_字符绘制.f_Draw(画布, &字体, box->m_背景绘制.m_属性);
}

static void on_标签变换修改(C_Widget* self) {
	C_标签* box = static_cast<C_标签*>(self);

	mat3X2 Mat;
	Mat.row0 = {};
	Mat.row1 = box->m_Size;
	Mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };

	box->m_背景绘制.f_alloc(1);
	box->m_背景绘制.f_set单色填充(0, Mat, self->m_颜色A, self->m_图元);
	
	//return;
	box->m_字符绘制.f_alloc(1, 32);
	box->m_字符绘制.f_绘制文本({ Mat.row0 , Mat.row1 }, S_UI主题::uic_文字颜色, 0, box->m_标签文本, E_对齐方式::e_据中对齐);
	
}

C_标签::C_标签(S_UI创建环境& ctx) : C_Widget(&ctx) {
	mf_DrawFun = on_标签Draw;
	mf_变换修改 = on_标签变换修改;

	m_颜色A = S_UI主题::uic_按钮背景;
	m_颜色B = S_UI主题::uic_按钮;

	m_图元 = S_Widget预设图元::m_圆角矩形;
}

C_标签::~C_标签() {
}



C_Widget* f_ui_创建标签(S_UI创建环境& ctx) {
	return new C_标签(ctx);
}

void f_ui_绑定标签属性(C_Widget* self, S_Props& prop) {
	C_标签* label = static_cast<C_标签*>(self);
	label->m_Value = prop;
	label->m_标签文本 = f_prop_Str(prop);
}









